package com.zhiyizhen.handheld.common.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;

import com.zhiyizhen.handheld.R;
import com.jcodecraeer.xrecyclerview.CustomFooterViewCallBack;
import com.jcodecraeer.xrecyclerview.progressindicator.AVLoadingIndicatorView;

public class LoadMoreLayout extends FrameLayout implements CustomFooterViewCallBack {

    protected ViewGroup mLoadingLayout;
    protected AVLoadingIndicatorView mLoading;
    protected TextView mLoadingTv;
    protected TextView mNoMoreTv;
    protected ViewGroup mEmptyLayout;
    protected ImageView mEmptyIv;
    protected TextView mEmptyTv;

    public LoadMoreLayout(@NonNull Context context) {
        this(context, null);
    }

    public LoadMoreLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadMoreLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        inflate(context, R.layout.layout_load_more, this);
        initView();
    }

    private void initView() {
        mLoadingLayout = findViewById(R.id.layout_loading);
        mLoading = findViewById(R.id.loading);
        mLoadingTv = findViewById(R.id.tv_loading);
        mNoMoreTv = findViewById(R.id.tv_no_more);
        mEmptyLayout = findViewById(R.id.layout_empty);
        mEmptyIv = findViewById(R.id.iv_empty);
        mEmptyTv = findViewById(R.id.tv_empty);
    }

    public void destroy() {
        if (mLoading != null) {
            mLoading.destroy();
            mLoading = null;
        }
    }

    @Override
    public void onLoadingMore(View yourFooterView) {
        mLoadingLayout.setVisibility(View.VISIBLE);

        mNoMoreTv.setVisibility(View.GONE);
        mEmptyLayout.setVisibility(View.GONE);

        resetLayoutHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setVisibility(View.VISIBLE);
    }

    @Override
    public void onLoadMoreComplete(View yourFooterView) {
        setVisibility(View.GONE);
    }

    @Override
    public void onSetNoMore(View yourFooterView, boolean noMore) {
        if (noMore) {
            mNoMoreTv.setVisibility(View.VISIBLE);

            mLoadingLayout.setVisibility(View.GONE);
            mEmptyLayout.setVisibility(View.GONE);

            resetLayoutHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
            setVisibility(View.VISIBLE);
        } else {
            setVisibility(View.GONE);
        }
    }

    public void showEmptyView() {
        mEmptyLayout.setVisibility(View.VISIBLE);

        mLoadingLayout.setVisibility(View.GONE);
        mNoMoreTv.setVisibility(View.GONE);

        resetLayoutHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        setVisibility(View.VISIBLE);
    }

    public void setEmptyView(@DrawableRes int iconId, @StringRes int textId) {
        if (iconId != 0) {
            mEmptyIv.setImageResource(iconId);
        }
        if (textId != 0) {
            mEmptyTv.setText(textId);
        }
    }

    private void resetLayoutHeight(int height) {
        ViewGroup.LayoutParams params = getLayoutParams();
        if (params == null) {
            return;
        }
        if (params.height != height) {
            params.height = height;
            setLayoutParams(params);
        }
    }

}
